package br.ufu.facom.calendar;

import java.util.Calendar;

import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

public class DateTimeHandler {
	private String dt;
	private String tm;
	private OnDateTimeListener onDateTimeListener;
	private TextView txtDT;
	private TextView txtTM;

	public DateTimeHandler() {

	}

	public DateTimeHandler(OnDateTimeListener onDateTimeListener) {
		this.onDateTimeListener = onDateTimeListener;
	}

	public DateTimeHandler(TextView txtDT, TextView txtTM) {
		this.txtDT = txtDT;
		this.txtTM = txtTM;
	}

	public void showDateDialog(FragmentManager fm, String tag, String title) {
		Log.d(this.getClass().getCanonicalName(), "showDateDialog");
		DatePickerFragment dtFragment = new DatePickerFragment();
		if (this.dt == null)
			setCalendar(Calendar.getInstance());
		dtFragment.setDateTimeHandler(this);
		dtFragment.setTitle(title);
		Log.d(this.getClass().getCanonicalName(), "showDateDialog>show");
		dtFragment.show(fm, tag);
	}

	public void showTimeDialog(FragmentManager fm, String tag, String title) {
		TimePickerFragment tmFragment = new TimePickerFragment();
		if (this.tm == null)
			setCalendar(Calendar.getInstance());
		tmFragment.setDateTimeHandler(this);
		tmFragment.setTitle(title);
		tmFragment.show(fm, tag);
	}

	public static String datePicker2String(DatePicker datePicker) {
		if (datePicker == null)
			return "";
		return String.valueOf(10000 + datePicker.getYear()).substring(1) + "-"
				+ String.valueOf(101 + datePicker.getMonth()).substring(1)
				+ "-"
				+ String.valueOf(100 + datePicker.getDayOfMonth()).substring(1);
	}

	public static boolean string2DatePicker(DatePicker datePicker, String s) {
		if (s.length() == 0)
			return false;
		datePicker.updateDate(Integer.parseInt(s.substring(0, 4)),
				Integer.parseInt(s.substring(5, 7)) - 1,
				Integer.parseInt(s.substring(8, 10)));
		return true;
	}

	public static String timePicker2String(TimePicker timePicker) {
		if (timePicker == null)
			return "";
		else
			return String.valueOf(100 + timePicker.getCurrentHour()).substring(
					1)
					+ ":"
					+ String.valueOf(100 + timePicker.getCurrentMinute())
							.substring(1);
	}

	public static boolean string2TimePicker(TimePicker timePicker, String s) {
		if (s.length() == 0)
			return false;
		timePicker.setCurrentHour(Integer.parseInt(s.substring(0, 2)));
		timePicker.setCurrentMinute(Integer.parseInt(s.substring(3, 5)));
		return true;
	}

	public void setDate(String dt) {
		this.dt = dt;
		if (this.onDateTimeListener != null)
			this.onDateTimeListener.onDateSet(this);
		if (this.txtDT != null)
			if (this.dt != null)
				this.txtDT.setText(this.dt);
			else
				this.txtDT.setText("n/d");
	}

	public void setDate(DatePicker datePicker) {
		this.dt = datePicker2String(datePicker);
		if (this.onDateTimeListener != null)
			this.onDateTimeListener.onDateSet(this);
		if (this.txtDT != null)
			this.txtDT.setText(this.dt);
	}

	public void setTime(String tm) {
		this.tm = tm;
		if (this.onDateTimeListener != null)
			this.onDateTimeListener.onTimeSet(this);
		if (this.txtTM != null)
			if (this.tm != null)
				this.txtTM.setText(this.tm);
			else
				this.txtTM.setText("n/d");
	}

	public void setTime(TimePicker timePicker) {
		this.tm = timePicker2String(timePicker);
		if (this.onDateTimeListener != null)
			this.onDateTimeListener.onTimeSet(this);
		if (this.txtTM != null)
			this.txtTM.setText(this.tm);
	}

	public void setCalendar(Calendar c) {
		// Use the current date as the default date in the picker
		this.dt = String.valueOf(10000 + c.get(Calendar.YEAR)).substring(1)
				+ "-"
				+ String.valueOf(101 + c.get(Calendar.MONTH)).substring(1)
				+ "-"
				+ String.valueOf(100 + c.get(Calendar.DAY_OF_MONTH)).substring(
						1);
		// Use the current time as the default values for the picker
		this.tm = String.valueOf(100 + c.get(Calendar.HOUR_OF_DAY))
				.substring(1)
				+ ":"
				+ String.valueOf(100 + c.get(Calendar.MINUTE)).substring(1);
		if (this.onDateTimeListener != null) {
			this.onDateTimeListener.onDateSet(this);
			this.onDateTimeListener.onTimeSet(this);
		}
		if (this.txtDT != null)
			this.txtDT.setText(this.dt);
		if (this.txtTM != null)
			this.txtTM.setText(this.tm);
	}

	public String getDate() {
		if (this.dt == null)
			return "";
		return this.dt;
	}

	public String getTime() {
		if (this.tm == null)
			return "";
		return this.tm;
	}

	public Calendar getCalendar() {
		Calendar c = Calendar.getInstance();
		if (this.dt != null) {
			c.set(Calendar.YEAR, Integer.parseInt(this.dt.substring(0, 4)));
			c.set(Calendar.MONTH, Integer.parseInt(this.dt.substring(5, 7)) - 1);
			c.set(Calendar.DAY_OF_MONTH,
					Integer.parseInt(this.dt.substring(8, 10)));
		}
		if (this.tm != null) {
			c.set(Calendar.HOUR, Integer.parseInt(this.tm.substring(0, 2)));
			c.set(Calendar.MINUTE, Integer.parseInt(this.tm.substring(3, 5)));
		}
		return c;
	}

	public interface OnDateTimeListener {
		public void onDateSet(DateTimeHandler dt);

		public void onTimeSet(DateTimeHandler dt);
	}

	public class OnDateTimeAdapter implements OnDateTimeListener {
		public void onDateSet(DateTimeHandler dt) {

		}

		public void onTimeSet(DateTimeHandler dt) {

		}
	}
}
